package cn.edu.neosoft.sunbo509.finefood.Fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Adapter.CollectdishesAdapter;
import cn.edu.neosoft.sunbo509.finefood.Adapter.MyDividerItemDecoration;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.GetcollectBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectdishesFragment extends Fragment {
    private View view;
    private RecyclerView rv_collectdishes;
    private CollectdishesAdapter adapter;
    private List<GetcollectBean>list;
    private Retrofit retrofit;
    private  String userid;
    private Service service;

    public CollectdishesFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_collectdishes, container, false);
        Bundle bundle=getArguments();
        userid=bundle.getString("userid");
        InitRecycleview();
        getDate();
        return view;
    }

    private void InitRecycleview() {
        rv_collectdishes=(RecyclerView)view.findViewById(R.id.rv_collectdishes);
        GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),1);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return 1;
            }
        });
        rv_collectdishes.setLayoutManager(layoutManager);
        adapter=new CollectdishesAdapter(getActivity());
        rv_collectdishes.setAdapter(adapter);
        rv_collectdishes.addItemDecoration(new MyDividerItemDecoration(getActivity(),GridLayoutManager.VERTICAL));
    }

    //获取收藏菜谱信息，flag=1;
    private void getDate() {
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
       service=retrofit.create(Service.class);
        Call<List<GetcollectBean>>call=service.getMessage(Integer.valueOf(userid),1);
        call.enqueue(new Callback<List<GetcollectBean>>() {
            @Override
            public void onResponse(Call<List<GetcollectBean>> call, Response<List<GetcollectBean>> response) {
                list=response.body();
                adapter.setList(list);
                adapter.setUser_id(userid);
            }

            @Override
            public void onFailure(Call<List<GetcollectBean>> call, Throwable t) {
                Toast.makeText(getActivity(), "异常", Toast.LENGTH_SHORT).show();
            }
        });

    }



}
